package com.netease.streamlearningbyjava;

import com.netease.streamlearningbyjava.bean.Trader;
import com.netease.streamlearningbyjava.bean.Transaction;

import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * 创建日期：2021/11/8 17:25
 *
 * @author tony.sun
 * 类说明：
 */

public class TestTransaction {
    List<Transaction> transactions=null;
    @Before
    public void before(){
        Trader raoul=new Trader("Raoul","Cambridge");
        Trader mario=new Trader("Mario","Milan");
        Trader alan=new Trader("Alan","Cambridge");
        Trader brian=new Trader("Brian","Cambridge");

        transactions= Arrays.asList(
          new Transaction(brian,2011,300),
          new Transaction(brian,2011,700),
          new Transaction(brian,2011,200),
          new Transaction(raoul,2012,1000),
          new Transaction(raoul,2011,400),
          new Transaction(mario,2012,710),
          new Transaction(mario,2012,700),
          new Transaction(alan,2012,950)
        );

    }
    @Test
    public void test1(){
        transactions.stream()
                .filter((e) -> e.getYear() == 2011)
                .sorted((e1,e2)->Integer.compare(e1.getValue(),e2.getValue()))
//                .sorted(Comparator.comparing(Transaction::getValue))
                .forEach(System.out::println);//这个可以
        System.out.println("---------------------------------------------------");
        transactions.stream()
                .map((e)->e.getTrader().getCity())
                .distinct().forEach(System.out::println);
        System.out.println("---------------------------------------------------");
        transactions.stream()
                .filter((e)->e.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getTrader)
                .sorted((e1,e2)->e1.getName().compareTo(e2.getName()))
                .distinct()
                .forEach(System.out::println);
        System.out.println("---------------------------------------------------");
        transactions.stream()
                .map(Transaction::getTrader)
                .map(Trader::getName)
                .sorted((e1,e2)->e1.compareTo(e2))
                .distinct()
                .forEach(System.out::println);
        System.out.println("---------------------------------------------------");
        transactions.stream()
                .map(Transaction::getTrader)
                .filter((e)->e.getCity().equals("Milan")).distinct().forEach(System.out::println);
        System.out.println("----------------------------------------");
        transactions.stream()
                .filter((e)->e.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getValue)
                .forEach(System.out::println);
        //总结:map类似地图，我们可以找到里面的某个位置（list里面的某个值），而总list相对于真实的场景（地球），
        Optional<Transaction> max = transactions.stream()
                .max((e1, e2) -> Integer.compare(e1.getValue(), e2.getValue()));
        System.out.println(max.get());
        System.out.println("-----------------------------------");
        Optional<Transaction> min = transactions.stream()
                .min((e1, e2) -> Integer.compare(e1.getValue(), e2.getValue()));
        System.out.println(min.get());
    }
}
